﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using ThanhCongJSC.Helper;
using ThanhCongJSC.Models;

namespace ThanhCongJSC.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index(string queryString, int? page)
        {
            if (string.IsNullOrEmpty(queryString))
                return View("GioiThieu");

            // chuc nang search
            if (queryString.Contains(Constant.StringTimKiem))
            {
                string strKeyword = queryString.Remove(0, 7);
                ViewData["keyword"] = strKeyword;
                ViewData["QueryString"] = queryString;

                List<Content> listContent = null;
                using (DBThangCongJSCEntities entity = new DBThangCongJSCEntities())
                {
                    ContentRepository contentRepository = new ContentRepository(entity);
                    listContent = contentRepository.GetListContentDetailByType("none").ToList<Content>();
                }

                //if (listContent == null)
                //    return View("GioiThieu");

                if (!string.IsNullOrEmpty(strKeyword))
                {
                    int i = 0;
                    do
                    {
                        var item = listContent[i];
                        string title2 = string.IsNullOrEmpty(item.ContentTitle2) ? string.Empty : item.ContentTitle2;
                        string qTitleVi = ThanhCongJSC.Helper.Util.GenerateCodeByTitle(strKeyword.ToLower());
                        string itemTitle = ThanhCongJSC.Helper.Util.GenerateCodeByTitle(item.ContentTitle.ToLower());
                        if (itemTitle.Contains(qTitleVi) || title2.Contains(strKeyword))
                        {
                            i++;
                            continue;
                        }

                        listContent.Remove(item);
                    } while (i < listContent.Count);
                }

                //if (listContent.Count <= 0)
                //    return View("GioiThieu");

                int pageIndex = (page ?? 1) - 1;
                PaginatedList<Content> model = new PaginatedList<Content>(
                    listContent, pageIndex, Constant.PageSize);

                return View("TimKiem", model);
            }

            // get the type of query string Static or Dynamic
            //  Static page   
            if (Constant.QueryStringStatic.ContainsKey(queryString))
                return View(Constant.QueryStringStatic[queryString]);

            //  ++ Dynamic page
            //  Du An & Tin Tuc
            string viewName = string.Empty;
            // danh sach tin tuc va danh sach du an
            if (queryString.Contains(Constant.StringDanhSach))
            {
                // get exactly query string except danh-sach
                queryString = queryString.Substring(Constant.StringDanhSach.Length + 1, queryString.Length - Constant.StringDanhSach.Length - 1);
                //  get page index for paging
                int lastIndex = queryString.LastIndexOf("-");
                string queryStringPage = queryString.Substring(lastIndex + 1, queryString.Length - lastIndex - 1);
                int pageIndex = -1;

                if (queryStringPage.Contains("page"))
                {
                    string pageIndexString = queryStringPage.Substring(4, queryStringPage.Length - 4);
                    if (!string.IsNullOrEmpty(pageIndexString))
                        pageIndex = int.Parse(pageIndexString) - 1;

                    queryString = queryString.Substring(0, lastIndex);
                }

                if (pageIndex == -1)
                    pageIndex = 0;

                PaginatedList<Content> model;
                using (DBThangCongJSCEntities entity = new DBThangCongJSCEntities())
                {
                    ContentRepository contentRepository = new ContentRepository(entity);
                    string cultureInfo = "";
                    if (Session["CultureInfo"] != null)
                        cultureInfo = Session["CultureInfo"].ToString();
                    var contents = contentRepository.GetListContentDetailByType(queryString, cultureInfo).OrderByDescending(b => b.PublishDate);
                    model = new PaginatedList<Content>(contents, pageIndex, Constant.PageSize);
                }
                viewName = Constant.QueryStringDuAn.Contains(queryString) ? "DuAn" : "TinTuc";

                ViewData["QueryString"] = queryString;
                return View(viewName, model);
            }

            //  Get Content detail
            ThanhCongJSC.Models.Content contentDetail = new Content();
            using (DBThangCongJSCEntities entity = new DBThangCongJSCEntities())
            {
                ContentRepository contentRepository = new ContentRepository(entity);
                string cultureInfo = "";
                if (Session["CultureInfo"] != null)
                    cultureInfo = Session["CultureInfo"].ToString();
                contentDetail = contentRepository.GetContentDetailByCode(queryString, cultureInfo);
            }

            //  Indetify viewName
            if (Constant.QueryStringTuyenDung.Contains(queryString))
                viewName = "TuyenDung";
            else if (queryString.IndexOf("tin-tuc-") == 0)
                viewName = "ChiTietTinTuc";
            else if (queryString.IndexOf("du-an-") == 0)
                viewName = "ChiTietDuAn";

            return View(viewName, contentDetail);
            //  -- Dynamic page
        }


    }
}
